package com.me.test.config;

        
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
import java.util.concurrent.atomic.AtomicInteger;

@Component
@Slf4j
public class RabbitConsumer {

   AtomicInteger count = new AtomicInteger();

   /**
   * 普通队列消费者
   * @param data
   * @param channel
   * @param tag
   * @throws Exception
   */
   @RabbitListener(queues="retry-queue")
  public void consumer(String data, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws Exception{

    log.info(">>>> consumer 消费 tag = {},次数count={},消息内容 : {}",tag, count.incrementAndGet(),data);
    // TODO 消息处理逻辑
    throw new RuntimeException("抛出异常，模拟消费失败，触发spring-retry");
   }

   /**
   * 死信队列消费者
   * @param data
   * @param channel
   * @param tag
   * @throws Exception
   */
  @RabbitListener(queues="my-queue-dl")
  public void consumeDL(String data, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws Exception{
    log.info(">>>> 死信队列消费 tag = {},消息内容 : {}",tag,data);
//     channel.basicNack(tag, false, false);
   }
}
